<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * @method whereIn(string $string, array $menuIds)
 * @method select(array|string[] $fields)
 * @method where(string $string, string $username)
 * @method count()
 * @method insert(array $params)
 * @method insertGetId(array $params)
 */
class BaseModel extends Model
{
    const QUERY_DEFAULT_FIELDS = ['id'];    // 默认字段

    /**
     * 删除状态
     */
    const DELETED_NO  = 0; //未删除
    const DELETED_YES = 1; //已删除

    /**
     * 表结构是否有删除字段
     *
     * @var bool
     */
    protected bool $tableFieldDeleted = true;

    public $timestamps = true;

    /**
     * 通过主键ID获取数据
     *
     * @param int            $id
     * @param array|string[] $fields
     * @return array
     */
    public function getItemById(int $id, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
            ->where('id', $id)
            ->where('deleted', self::DELETED_NO)
            ->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 通过一组主键ID获取数据
     *
     * @param array          $ids
     * @param array|string[] $fields
     * @return array
     */
    public function getItemsByIds(array $ids, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
            ->whereIn('id', $ids)
            ->where('deleted', self::DELETED_NO)
            ->get()
            ->toArray();
    }

    /**
     * 添加数据【可添加多条】
     *
     * @return void
     */
    public function addItem(array $params)
    {
        if (isset($params[0]) && is_array($params[0])) {
            foreach ($params as $index => $item) {
                $params[$index]['deleted']    = self::DELETED_NO;
                $params[$index]['created_at'] = DATE_NOW;
                $params[$index]['updated_at'] = DATE_NOW;
            }
        } else {
            $params['deleted']    = self::DELETED_NO;
            $params['created_at'] = DATE_NOW;
            $params['updated_at'] = DATE_NOW;
        }

        return $this->insert($params);
    }

    /**
     * 添加数据并获取主键id
     *
     * @param array $params
     * @return int
     */
    public function addItemGetId(array $params): int
    {
        if (empty($params['deleted'])) {
            $params['deleted'] = self::DELETED_NO;
        }
        if (empty($params['created_at'])) {
            $params['created_at'] = DATE_NOW;
        }
        if (empty($params['updated_at'])) {
            $params['updated_at'] = DATE_NOW;
        }

        return $this->insertGetId($params);
    }

    /**
     * 根据id更新数据
     *
     * @return void
     */
    public function updateItemById(int $id, array $updateData)
    {
        return $this->where('id', $id)
            ->update($updateData);
    }

    /**
     * 通过id将数据标记为删除
     *
     * @param int $id
     * @return int
     */
    public function deleteItemById(int $id): int
    {
        return $this->where('id', $id)
            ->where('deleted', self::DELETED_NO)
            ->update([
                'deleted' => self::DELETED_YES,
            ]);
    }

    /**
     * 通过一组id将数据标记为删除
     *
     * @param array $ids
     * @return mixed
     */
    public function deleteItemsByIds(array $ids)
    {
        return $this->whereIn('id', $ids)
            ->where('deleted', self::DELETED_NO)
            ->update([
                'deleted' => self::DELETED_YES,
            ]);
    }
}
